package cn.jeeweb.common.utils;


import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

/**
 * @package cn.jeeweb.common.utils
 * @title:
 * @description: 获取地址类
 */
public class AddressUtils
{
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";
    private static  final String CACHENAME="ip.taobao.com";
    private static final int timeToIdleSeconds=7*24*60*60;

    public static String getRealAddressByIP(String ip)
    {
        String address =(String) (CacheUtils.get(CACHENAME,ip));
        if(address==null || address.isEmpty()) {
            try {
                Map<String, Object> params = new HashMap<>();
                params.put("ip", ip);
                Map<String, Object> headers = new HashMap<>();
                headers.put("accept", "*/*");
                headers.put("connection", "Keep-Alive");
                headers.put("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                String rspStr = HttpClientUtils.httpGetRequest(IP_URL, headers, params);
                if (StringUtils.isEmpty(rspStr)) {
                    log.error("获取地理位置异常 {}", ip);
                    //return address;
                    address="无地理位置信息";
                }
                else {
                    JSONObject obj = JSONObject.parseObject(rspStr);
                    JSONObject data = obj.getObject("data", JSONObject.class);
                    String region = data.getString("region");
                    String city = data.getString("city");
                    address = region + " " + city;
                }
                CacheUtils.put(CACHENAME, ip, address,timeToIdleSeconds);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return address;
    }
}

